using UnityEngine;
using System.Collections;

public class StandardHUD : MonoBehaviour {

    private GameManager gm;

    /* States:
     *      -normal: show the # of living robots/max
     *      -paused: gray out everything in the scene, and the HUD. Show pause menu.
     *          - should continue drawing the normal menu underneath the paused one
     * 
     
     */

    private bool isPaused;

    public bool IsPaused
    {
        get { return isPaused; }
    }

    void Start()
    {
        gm = GameObject.Find("GameManager").GetComponent<GameManager>();

        Debug.Log("Won? " + gm.Won().ToString() + " Lost? " + gm.Lost().ToString());

        isPaused = false;
        Time.timeScale = 1.0f;
    }

    void Update()
    {
        // Only allow pausing if we didnt win or lose already.
        if (Input.GetButtonDown("Pause") && !gm.Won() && !gm.Lost())
        {
            Debug.Log("Paused");
            isPaused = !isPaused;
        }

        if (isPaused || gm.Won() || gm.Lost())
            Time.timeScale = 0;
        else
            Time.timeScale = 1.0f;
    }

    void defaultMenu()
    {
        GUI.Box(new Rect(0, 0, 120, 60), GUIContent.none);
        GUI.Label(new Rect(10, 10, 100, 20), "Robots Saved: " + gm.RobotsSaved.ToString());
        GUI.Label(new Rect(10, 30, 100, 20), "Robots Left: " + gm.RobotsAlive.ToString());
    }

    void pauseMenu()
    {
        // Draw a box the size of the screen
        GUI.Box(new Rect(0, 0, Screen.width, Screen.height), GUIContent.none);
        GUI.Label(new Rect(Screen.width / 2 - 20, Screen.height / 2, 50, 20), "Paused");

        if (GUI.Button(new Rect(Screen.width / 2 - 140, Screen.height - 200, 280, 40), "Return to Game"))
        {
            isPaused = false;
        }
        if (GUI.Button(new Rect(Screen.width / 2 - 140, Screen.height - 100, 280, 40), "Quit"))
        {
            Application.LoadLevel(0); // main menu
        }

    }

    void gameOverMenu()
    {
        // Draw a box the size of the screen
        GUI.Box(new Rect(0, 0, Screen.width, Screen.height), GUIContent.none);
        GUI.Label(new Rect(Screen.width / 2 - 20, Screen.height / 2, 100, 20), "You LOST!?");

        if (GUI.Button(new Rect(Screen.width / 2 - 140, Screen.height - 200, 280, 40), "Retry"))
        {
            Application.LoadLevel(Application.loadedLevel);
        }
        if (GUI.Button(new Rect(Screen.width / 2 - 140, Screen.height - 100, 280, 40), "Quit"))
        {
            Application.LoadLevel(0); // main menu
        }
    }

    void winMenu()
    {
        // Draw a box the size of the screen
        GUI.Box(new Rect(0, 0, Screen.width, Screen.height), GUIContent.none);
        GUI.Label(new Rect(Screen.width / 2 - 20, Screen.height / 2, 100, 20), "You WON!?");

        if (GUI.Button(new Rect(Screen.width / 2 - 140, Screen.height - 200, 280, 40), "Main Menu"))
        {
            Application.LoadLevel(0); // main menu
        }
    }

    void OnGUI()
    {
        defaultMenu();
        if (gm.Lost())
        {
            gameOverMenu();
        }
        else if (gm.Won())
        {
            winMenu();
        }
        else if (isPaused)
            pauseMenu();



    }
}
